import { ElMessage } from 'element-plus'
import { resBody } from '@/hooks/utils'
import { cartoonDetail } from '@/network/model/cartoonModel'


export default class CartoonService {
  api: any;
  constructor(api: any) {
    this.api = api
  }
  /**
   * 
   * @param id 根据id获取详情
   * @returns 
   */
  async getCartoonDetail(id: number, deliveryName: string) {
    try {
      const res = await this.api[deliveryName]({ id })
      console.log(res);
      if (res.code !== 200) {
        ElMessage.error(res.msg)
        return resBody(201, res.data)
      }
      return resBody(200, res.data)
    } catch (error) {
      ElMessage.error('网络接口错误')
      return resBody(201, error)
    }
  }
  /**
   * 
   * @param param 修改漫画信息
   */
  async editRow(param: cartoonDetail, deliveryName: string) {
    try {
      const res = await this.api[deliveryName](param)
      console.log(typeof res.code, res.code);
      if (res.code == 200) {
        ElMessage.success(res.msg)
      } else {
        ElMessage.error(res.msg)

      }
    } catch (error) {
      ElMessage.error('修改失败')
    }
  }
  /**
   * 
   * @param comic_id 漫画id 根据id获取章节列表
   * @param type 默认获取实时数据，1：开启缓存
   * @returns 
   */
  async getChapterAll(comic_id: number, deliveryName: string, type: number = 0) {
    try {
      const pageSize = 20
      let res = [] as any;
      if (type != 1) {
        res = await this.api[deliveryName]({ comic_id, isAll: true, pageSize, page: Math.round(Math.random() * 1000) })
      } else {
        res = await this.api[deliveryName]({ comic_id, isAll: true })
      }

      if (res.code !== 200) {
        ElMessage.error(res.msg)
        return resBody(201, res.data)
      }
      console.log(res);
      return resBody(200, res.data.data)
    } catch (error) {
      ElMessage.error('网络接口错误')
      return resBody(201, error)
    }
  }
}